<template>

<ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="合同编号" prop="htid">
        <el-input
          v-model="queryParams.htid"
          placeholder="请输入合同编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="阶段号" prop="jdh">
        <el-input
          v-model="queryParams.jdh"
          placeholder="请输入阶段号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="收付款类型" prop="sfklx">
        <el-select
          v-model="queryParams.sfklx"
          placeholder="请选择收付款类型"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.CONT_SFKFS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="收付款方向" prop="sfkfx">
        <el-select
          v-model="queryParams.sfkfx"
          placeholder="请选择收付款方向"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.CONT_SFKFX)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="应收付日期" prop="ysfrq">
        <!-- <el-date-picker
          v-model="queryParams.ysfrq"
          value-format="YYYY-MM-DD"
          type="date"
          placeholder="选择应收付日期"
          clearable
          class="!w-240px"
        /> -->
        <el-date-picker
          v-model="queryParams.ysfrq"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="实际应收付日期" prop="sjysfrq">
        <el-date-picker
          v-model="queryParams.sjysfrq"
          value-format="YYYY-MM-DD"
          type="date"
          placeholder="选择实际应收付日期"
          clearable
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="差额原因" prop="ceyy">
        <el-input
          v-model="queryParams.ceyy"
          placeholder="请输入差额原因"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="支付方式" prop="zffs">
        <el-input
          v-model="queryParams.zffs"
          placeholder="请输入支付方式"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="收付款单据号" prop="djh">
        <el-input
          v-model="queryParams.djh"
          placeholder="请输入收付款单据号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="结算时间" prop="jssj">
        <el-date-picker
          v-model="queryParams.jssj"
          value-format="YYYY-MM-DD"
          type="date"
          placeholder="选择结算时间"
          clearable
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="结算单据号" prop="jsdjh">
        <el-input
          v-model="queryParams.jsdjh"
          placeholder="请输入结算单据号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="是否开具发票" prop="sfkjfp">
        <el-select
          v-model="queryParams.sfkjfp"
          placeholder="请选择是否开具发票"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发票号" prop="fph">
        <el-input
          v-model="queryParams.fph"
          placeholder="请输入发票号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="开票单位" prop="khfzr">
        <el-input
          v-model="queryParams.khfzr"
          placeholder="请输入开票单位"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="发票金额" prop="fpjr">
        <el-input
          v-model="queryParams.fpjr"
          placeholder="请输入发票金额"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="经手人" prop="jsr">
        <el-input
          v-model="queryParams.jsr"
          placeholder="请输入经手人"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="票据运送方式" prop="pjysfs">
        <el-input
          v-model="queryParams.pjysfs"
          placeholder="请输入票据运送方式"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="票据运输号" prop="pjysh">
        <el-input
          v-model="queryParams.pjysh"
          placeholder="请输入票据运输号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="备注" prop="bz">
        <el-input
          v-model="queryParams.bz"
          placeholder="请输入备注"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['cont:pay-info:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['cont:pay-info:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="合同编号" align="center" prop="htid" />
      <el-table-column label="阶段号" align="center" prop="jdh" />
      <el-table-column label="收付款类型" align="center" prop="sfklx">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CONT_SFKFS" :value="scope.row.sfklx" />
        </template>
      </el-table-column>
      <el-table-column label="收付款方向" align="center" prop="sfkfx">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CONT_SFKFX" :value="scope.row.sfkfx" />
        </template>
      </el-table-column>
      <el-table-column label="应收付金额" align="center" prop="ysfje" />
      <el-table-column label="实际应收付金额" align="center" prop="sjysfje" />
      <el-table-column label="实际收付金额" align="center" prop="sjsfje" />
      <el-table-column label="未收付金额" align="center" prop="wsfje" />
      <el-table-column label="应收付日期" align="center" prop="ysfrq" :formatter="dateFormatter" />
      <el-table-column label="实际应收付日期" align="center" prop="sjysfrq" :formatter="dateFormatter" />
      <el-table-column label="差额原因" align="center" prop="ceyy" />
      <el-table-column label="支付方式" align="center" prop="zffs" />
      <el-table-column label="收付款单据号" align="center" prop="djh" />
      <el-table-column
        label="结算时间"
        align="center"
        prop="jssj"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="结算单据号" align="center" prop="jsdjh" />
      <el-table-column label="是否开具发票" align="center" prop="sfkjfp">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.sfkjfp" />
        </template>
      </el-table-column>
      <el-table-column label="发票号" align="center" prop="fph" />
      <el-table-column label="开票单位" align="center" prop="khfzr" />
      <el-table-column label="发票金额" align="center" prop="fpjr" />
      <el-table-column label="经手人" align="center" prop="jsr" />
      <el-table-column label="票据运送方式" align="center" prop="pjysfs" />
      <el-table-column label="票据运输号" align="center" prop="pjysh" />
      <el-table-column label="备注" align="center" prop="bz" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['cont:pay-info:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['cont:pay-info:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <PayInfoForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { getStrDictOptions, getBoolDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import * as PayInfoApi from '@/api/cont/pay'
import PayInfoForm from './PayInfoForm.vue'

defineOptions({ name: 'PayInfo' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  htid: undefined,
  jdh: undefined,
  sfklx: undefined,
  sfkfx: undefined,
  ysfje: undefined,
  sjysfje: undefined,
  sjsfje: undefined,
  wsfje: undefined,
  ysfrq: undefined,
  ysfrq: [],
  sjysfrq: undefined,
  sjysfrq: [],
  ceyy: undefined,
  zffs: undefined,
  djh: undefined,
  jssj: undefined,
  jssj: [],
  jsdjh: undefined,
  sfkjfp: undefined,
  fph: undefined,
  khfzr: undefined,
  fpjr: undefined,
  jsr: undefined,
  pjysfs: undefined,
  pjysh: undefined,
  bz: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

// 格式化日期为 "yyyy-MM-dd HH:mm:ss"
const formatDate = (date: Date) => {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  return `${year}-${month}-${day}`;
};
// 获取当前日期和一周前的日期
const today = new Date();
const oneWeekAgo = new Date(today);
oneWeekAgo.setDate(today.getDate() - 7);
// 使用 ref 创建响应式数据
const dateRange = ref<string[]>([
  formatDate(oneWeekAgo) + ' 00:00:00',
  formatDate(today) + ' 23:59:59',
]);
// 在组件挂载后打印 dateRange 值
// onMounted(() => {
//   console.log(dateRange.value);
// });
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await PayInfoApi.getPayInfoPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await PayInfoApi.deletePayInfo(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await PayInfoApi.exportPayInfo(queryParams)
    download.excel(data, '合同收付款记录表 .xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>